package com.suray.basic.wcs.node.bo;

import org.springframework.util.CollectionUtils;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class WCSNodeModuleBO {


    private int x;
    private int y;
    private int z;

	private Object lockBy;

	private Map<String, WCSNodeModuleRouteBO> routeBy;

	private WCSNodeModuleRouteBO maxLockTimeWCSNodeModuleRouteBO;

	public WCSNodeModuleBO() {
	    routeBy = new ConcurrentHashMap<>();
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getZ() {
        return z;
    }

    public void setZ(int z) {
        this.z = z;
    }

    public Object getLockBy() {
        return lockBy;
    }

    public void setLockBy(Object lockBy) {
        this.lockBy = lockBy;
    }

    public Map<String, WCSNodeModuleRouteBO> getRouteBy() {
        return routeBy;
    }

    public WCSNodeModuleRouteBO getMaxLockTimeWCSNodeModuleRouteBO() {
        return maxLockTimeWCSNodeModuleRouteBO;
    }

    public void addRouteBy(String key, WCSNodeModuleRouteBO wcsNodeModuleRouteBO){
        this.routeBy.put(key, wcsNodeModuleRouteBO);
        int maxLockTime = 0;
        for (Map.Entry<String, WCSNodeModuleRouteBO> entry : this.routeBy.entrySet()) {
            WCSNodeModuleRouteBO routeByItem = entry.getValue();
            if(routeByItem != null) {
                Integer lockTime = routeByItem.getLockTime();
                if(maxLockTime <= 0) {
                    maxLockTime = lockTime;
                    this.maxLockTimeWCSNodeModuleRouteBO = routeByItem;
                }
                if(maxLockTime > lockTime) {
                    maxLockTime = lockTime;
                    this.maxLockTimeWCSNodeModuleRouteBO = routeByItem;
                }
            }
        }
    }
    public void removeRouteBy(String key){
        this.routeBy.remove(key);
        int maxLockTime = 0;
        if (!CollectionUtils.isEmpty(this.routeBy)) {
            for (Map.Entry<String, WCSNodeModuleRouteBO> entry : this.routeBy.entrySet()) {
                WCSNodeModuleRouteBO routeByItem = entry.getValue();
                if(routeByItem != null) {
                    int lockTime = routeByItem.getLockTime();
                    if(maxLockTime <= 0) {
                        maxLockTime = lockTime;
                        this.maxLockTimeWCSNodeModuleRouteBO = routeByItem;
                    }
                    if(maxLockTime > lockTime) {
                        maxLockTime = lockTime;
                        this.maxLockTimeWCSNodeModuleRouteBO = routeByItem;
                    }

                }
            }
        } else {
            this.maxLockTimeWCSNodeModuleRouteBO.setPassTime(0);
            this.maxLockTimeWCSNodeModuleRouteBO.setLockTime(0);
            this.maxLockTimeWCSNodeModuleRouteBO.setCurrentPassTime(0);
        }
    }

}
